package rd.system.home {
	
	import com.greensock.TweenLite;
	import com.sff.core.ILoadable;
	import com.sff.module.Module;
	import com.sff.module.ModuleLoader;
	import com.sff.net.LoaderItem;
	import com.sff.SFFCore;
	import com.sff.site.core.INode;
	import com.sff.site.events.NodeEvent;
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author cuong.tran@rdsystem.org
	 */
	public class Home extends Module {
		
		public var mcFooter:Footer;
		public var mcBG:MovieClip;
		public var mcHolder:MovieClip;
		
		private var bmpBG:Bitmap;
		
		public function Home() {				
			mcFooter.x = 0;
			
			mcBG.x = mcBG.y = 0;
			mcBG.alpha = 0;
		}
		
		override protected function run():void {			
			mcFooter.setData(this.system.dataManager.xmlData.footer[0], this.system.styleManager);
			
			this.system.siteManager.addEventListener(NodeEvent.CHILD_CHANGE, onChildChangeHandler);
			onChildChangeHandler(null);
		}
		
		override public function getLoadables():ILoadable {
			
			var item:LoaderItem = new LoaderItem(new URLRequest(this.system.dataManager.xmlData.content.background.toString()));
			item.addEventListener(Event.COMPLETE, onCompleteLoadBGHandler);
			
			return item;
		}
		
		private function onCompleteLoadBGHandler(evt:Event):void {
			
			bmpBG = evt.currentTarget.loader.content as Bitmap;
			bmpBG.smoothing = true;
			addChildAt(bmpBG, 0);
			
			bmpBG.alpha = 0;
			TweenLite.to(bmpBG, 0.75, { alpha: 1 } );
			
			this.system.resizeManager.addEventListener(Event.RESIZE, onResizeHandler);			
			onResizeHandler(null);
		}
		
		private function onChildChangeHandler(evt:NodeEvent):void {
			
			var activeNode:INode = this.system.siteManager.getCurrentChild();
			activeNode = activeNode ? activeNode : this.system.siteManager.getDefaultNode();
			
			if (activeNode) {
				loadModule(this.system.dataManager.xmlData.menus.menu.(@nodeid == activeNode.getId()).url.toString());
			}
		}
		
		private function loadModule(url:String):void {			
			while (mcHolder.numChildren) {
				mcHolder.removeChildAt(0);
			}
			
			var ldr:ModuleLoader = this.system.getModuleLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteLoadModuleHandler);
			ldr.load(new URLRequest(url), this.system.dataManager);
		}
		
		private function onCompleteLoadModuleHandler(evt:Event):void {			
			mcHolder.addChild(evt.currentTarget.module);
		}
		
		private function onResizeHandler(evt:Event):void {
			if (bmpBG) {
				bmpBG.x = this.system.resizeManager.width / 2 - bmpBG.width / 2;
				bmpBG.y = this.system.resizeManager.height / 2 - bmpBG.height / 2;
			}
			
			mcBG.width = this.system.resizeManager.width;
			mcBG.height = this.system.resizeManager.height;
			
			mcFooter.resize(this.system.resizeManager.width, this.system.resizeManager.height);			
			mcFooter.y = this.system.resizeManager.height - mcFooter.height;
		}		
	}
}